package com.spring.study.bean.definition;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.annotation.PostConstruct;

/**
 * {@link BeanCreationException}
 */
public class BeanCreationExceptionDemo {

    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        //注册BeanDefinition Bean Class是一个 CharSequence接口
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(POJO.class);
        applicationContext.registerBeanDefinition("errorBean",beanDefinitionBuilder.getBeanDefinition());

        //启动应用上下文
        applicationContext.refresh();

        //关闭一能用上下文
        applicationContext.close();
    }


    static class POJO implements InitializingBean {

        @PostConstruct
        public void init() throws Throwable {
            throw new Exception("init(): For purposes.....");
        }

        @Override
        public void afterPropertiesSet() throws Exception {
            throw new Exception("For purcess......");
        }
    }
}
